package Interfaz;
import java.util.Vector;
import Modelo.Ingredient;
import Modelo.Step;
import Modelo.Util;
import Modelo.Revision;
import Modelo.Author;
import java.io.File;
import Modelo.FileXML;
import ingredientsClient.Consumer;

public class IngredientsGui extends javax.swing.JFrame {

    private Vector<String> tags;
    private Vector<Author> authors;
    private Vector<Step> steps;
    private Vector<Revision> revisions;
    
    private boolean modificando;
    // si estamos modificando, este atributo tiene
    // el ingrediente original, para comparar si hubo cambios
    private Ingredient ingredient;
    
    public IngredientsGui() {
        initComponents();
        tags = new Vector<String>();
        authors = new Vector<Author>();
        steps = new Vector<Step>();
        revisions = new Vector<Revision>();
        
        modificando = false;
    }
    
    public IngredientsGui( Ingredient ingredient ) {
        initComponents();
        tags = new Vector<String>();
        authors = new Vector<Author>();
        steps = new Vector<Step>();
        revisions = new Vector<Revision>();
        this.lHeader.setText( "Modify Ingredient" );
        
        modificando = true;
        this.ingredient = ingredient;
        loadModify( ingredient );
    }
    
    private void loadModify(Ingredient ingredient)
    {
        jTxtTitle.setText(ingredient.getTitle());
        jTxtAbstract.setText(ingredient.getDescription());
        steps = ingredient.getSteps();
        tags = ingredient.getTags();
        authors = ingredient.getAuthors();
        revisions = ingredient.getRevisions();
        IngredientsGui.this.modificando = true;
        IngredientsGui.this.ingredient = ingredient;
    }

    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        lHeader = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTxtTitle = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTxtAbstract = new javax.swing.JTextArea();
        bOk = new javax.swing.JButton();
        bCancel = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        jSeparator1 = new javax.swing.JSeparator();
        jBtnMakeStep = new javax.swing.JButton();
        jBtnViewSteps = new javax.swing.JButton();
        bManageAuthors = new javax.swing.JButton();
        bManageTags = new javax.swing.JButton();
        jMView = new javax.swing.JMenuBar();
        mView = new javax.swing.JMenu();
        mPreview = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        lHeader.setFont(new java.awt.Font("Tahoma", 1, 14));
        lHeader.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lHeader.setText("New Ingredient");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Title");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Abstract");

        jTxtAbstract.setColumns(20);
        jTxtAbstract.setLineWrap(true);
        jTxtAbstract.setRows(5);
        jTxtAbstract.setWrapStyleWord(true);
        jScrollPane1.setViewportView(jTxtAbstract);

        bOk.setText("Ok");
        bOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bOkActionPerformed(evt);
            }
        });

        bCancel.setText("Cancel");
        bCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelActionPerformed(evt);
            }
        });

        jToolBar1.setFloatable(false);

        jBtnMakeStep.setText("New Step");
        jBtnMakeStep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnMakeStepActionPerformed(evt);
            }
        });

        jBtnViewSteps.setText("View Steps");
        jBtnViewSteps.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnViewStepsActionPerformed(evt);
            }
        });

        bManageAuthors.setText("ManageAuthors");
        bManageAuthors.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bManageAuthorsActionPerformed(evt);
            }
        });

        bManageTags.setText("Manage Tags");
        bManageTags.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bManageTagsActionPerformed(evt);
            }
        });

        mView.setText("View");
        mPreview.setText("Preview");
        mPreview.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mPreviewActionPerformed(evt);
            }
        });

        mView.add(mPreview);

        jMView.add(mView);

        setJMenuBar(jMView);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(lHeader, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel3)
                                        .addComponent(jLabel2))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTxtTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE))))
                            .addContainerGap())
                        .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jBtnMakeStep, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE))
                                .addComponent(bManageTags, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jBtnViewSteps, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(bManageAuthors, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
                            .addContainerGap()))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(bOk, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bCancel)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lHeader)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTxtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bManageTags)
                    .addComponent(bManageAuthors))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnMakeStep)
                    .addComponent(jBtnViewSteps))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bCancel)
                    .addComponent(bOk))
                .addGap(45, 45, 45)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-337)/2, (screenSize.height-310)/2, 337, 310);
    }// </editor-fold>//GEN-END:initComponents

    private void mPreviewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mPreviewActionPerformed
        String path = Util.selectPath(1);
        
        File file = new File(path);
        
        Util.transformar(file);
    }//GEN-LAST:event_mPreviewActionPerformed

    private void bManageTagsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bManageTagsActionPerformed
        NewTagsDialog tagDialog = new NewTagsDialog( this, true );
        tagDialog.setTags( tags );
        tagDialog.setVisible( true );
        
        if( tagDialog.isSuccess() )
        {
            tags = tagDialog.getTags();
        }
    }//GEN-LAST:event_bManageTagsActionPerformed

    private void bManageAuthorsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bManageAuthorsActionPerformed
        NewAuthorsDialog authorsDialog = new NewAuthorsDialog( this, true );
        authorsDialog.setAuthors( authors );
        authorsDialog.setVisible( true );
        
        if( authorsDialog.isSuccess() )
        {
            authors = authorsDialog.getAuthors();
        }
    }//GEN-LAST:event_bManageAuthorsActionPerformed

    private void bCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_bCancelActionPerformed

    private void bOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOkActionPerformed
        int rev = 1;
        
        if( this.modificando )
        {
            rev = ingredient.getRevision() + 1;
        }
        
        if(authors.size() == 0){
            Util.errorDialog("Error", "No authors");
            return;
        }
        if(tags.size() == 0){
            Util.errorDialog("Error", "No tags");
            return;
        }
        if(steps.size() == 0){
            Util.errorDialog("Error", "No steps");
            return;
        }
        if(jTxtTitle.getText().compareTo("") == 0){
            Util.errorDialog("Error", "No Title");
            return;
        }
        
        if(jTxtAbstract.getText().compareTo("") == 0){
            Util.errorDialog("Error", "No Abstract");
            return;
        }
        
        
        java.util.Date date = new java.util.Date();
        Ingredient ingredient = new Ingredient(jTxtTitle.getText(), (int)date.getTime(), jTxtAbstract.getText(), tags, steps,  authors, revisions, rev);
        
        if( this.modificando )
        {
            Revision r = newRevision( rev );
            
            if( r == null )
            {
                Modelo.Util.errorDialog( "Error", "A revision must be created to save the modifications" );
                return;
            }
            
            ingredient.addRevision( r );
        }

        ingredient.save();
        
        File fiIngre = new File( ingredient.getFileName() );
        FileXML fxml = new FileXML( fiIngre );
        
        Consumer c = new Consumer();
        if( !c.sendFile( Util.node2String( fxml.toXml() ), false ) ){
            Util.errorDialog( "Connection Failed", "The new Ingredient cannot be uploaded." );
        }

        setVisible( false );
        dispose();
    }//GEN-LAST:event_bOkActionPerformed

    public Revision newRevision( int revisionNumber )
    {
        CreateRevision createRevision = new CreateRevision( this, true, revisionNumber, new java.util.Date() );
        createRevision.setVisible( true );
        return createRevision.getRevision();
    }
    
    private void jBtnViewStepsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnViewStepsActionPerformed

        if( steps.size() > 0 )
        {
            StepGui stepg = new StepGui(this, true, null);
            stepg.ViewSteps(steps);
            stepg.setVisible(true);
        }
        else
        {
            Util.errorDialog( "Error", "no steps available" );
        }
    }//GEN-LAST:event_jBtnViewStepsActionPerformed

    private void jBtnMakeStepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnMakeStepActionPerformed
        Step ste = new Step("", "");
        StepGui stepg = new StepGui(this, true, ste);
        stepg.setVisible(true);
        if(!(ste.getTitle().equalsIgnoreCase(""))){
            steps.addElement(ste);
        }
    }//GEN-LAST:event_jBtnMakeStepActionPerformed
  
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCancel;
    private javax.swing.JButton bManageAuthors;
    private javax.swing.JButton bManageTags;
    private javax.swing.JButton bOk;
    private javax.swing.JButton jBtnMakeStep;
    private javax.swing.JButton jBtnViewSteps;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenuBar jMView;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextArea jTxtAbstract;
    private javax.swing.JTextField jTxtTitle;
    private javax.swing.JLabel lHeader;
    private javax.swing.JMenuItem mPreview;
    private javax.swing.JMenu mView;
    // End of variables declaration//GEN-END:variables
    
}
